package com.weimin.ghostdrop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weimin.ghostdrop.entity.dto.SoftwareDTO;
import com.weimin.ghostdrop.entity.vo.SoftwareDetailVO;
import com.weimin.ghostdrop.entity.vo.SoftwareListVO;
import com.weimin.ghostdrop.service.SoftwareService;
import com.weimin.ghostdrop.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author : huiMing
 * Date : 2025年10月11日 10:41
 * @version V1.0
 */

@RestController
@RequestMapping("/software")
@RequiredArgsConstructor
public class SoftwareController {

    private final SoftwareService softwareService;

    // 新增，返回ID；同时写入 images 与 links
    @PostMapping("add")
    public R<?> create(@Validated @RequestBody SoftwareDTO dto) {
        return R.success(softwareService.createSoftware(dto));
    }

    // 修改：覆盖软件基本信息；若传 imageUrls/links 则覆盖更新
    @PutMapping("/{id}")
    public Boolean update(@PathVariable Integer id, @Validated @RequestBody SoftwareDTO dto) {
        softwareService.updateSoftware(id, dto);
        return true;
    }

    // 删除：级联删除 images 与 links
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        softwareService.deleteSoftware(id);
        return true;
    }

    // 分页查询全部（含主图），不带筛选
    @GetMapping("/list")
    public IPage<SoftwareListVO> page(@RequestParam(defaultValue = "1") long pageNum,
                                      @RequestParam(defaultValue = "10") long pageSize) {
        return softwareService.pageWithCover(new Page<>(pageNum, pageSize), null, null);
    }

    // 标题/编号 模糊搜索分页
    @GetMapping("/search")
    public IPage<SoftwareListVO> search(@RequestParam(defaultValue = "1") long pageNum,
                                        @RequestParam(defaultValue = "10") long pageSize,
                                        @RequestParam(required = false) String title,
                                        @RequestParam(required = false) String code) {
        return softwareService.pageWithCover(new Page<>(pageNum, pageSize), title, code);
    }

    // 详情：返回所有信息 + 全部图片
    @GetMapping("/detail/{id}")
    public SoftwareDetailVO detail(@PathVariable Integer id) {
        return softwareService.detail(id);
    }
}
